package com.turing.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.turing.domain.RepositoryInfo;
import com.turing.domain.RepositoryInfo;
import com.turing.service.IRepositoryInfoService;
import com.turing.utils.AjaxResult;
import com.turing.utils.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("repositoryInfo")
public class RepositoryInfoController {

    @Autowired
    private IRepositoryInfoService repositoryInfoService;

    /**
     * 查所有
     */
    @GetMapping("/list")
    public AjaxResult selectAll() {
        List<RepositoryInfo> list = repositoryInfoService.selectAll();
        return AjaxResult.success(list);
    }

    /**
     * 根据id 查一条
     */
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable() Long id) {
        RepositoryInfo repositoryInfo = repositoryInfoService.selectById(id);
        return AjaxResult.success(repositoryInfo);
    }

    /**
     * 查分页
     */
    @GetMapping("/page")
    public TableDataInfo selectPage(RepositoryInfo repositoryInfo) {
        PageHelper.startPage(repositoryInfo.getPageNum(), repositoryInfo.getPageSize());
        System.out.println("repositoryInfo = " + repositoryInfo);
        List<RepositoryInfo> list = repositoryInfoService.selectPage(repositoryInfo);

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(new PageInfo<>(list).getTotal());
        tableDataInfo.setRows(list);
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("success");
        return tableDataInfo;
    }
    /**
     * 添加
     */
    @PostMapping()
    public AjaxResult insertOne(@RequestBody RepositoryInfo repositoryInfo) {
        int i = repositoryInfoService.insertOne(repositoryInfo);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 修改
     */
    @PutMapping()
    public AjaxResult updateById(@RequestBody RepositoryInfo repositoryInfo) {
        int i = repositoryInfoService.updateById(repositoryInfo);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{ids}")
    public AjaxResult deleteByIds(@PathVariable() Long[] ids) {
        int i = repositoryInfoService.deleteByIds(ids);
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}